package com.slwish.common.utils;

import android.app.Activity;
import android.content.Context;
import android.os.IBinder;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;

/**
 * 键盘处理
 * Created by Mao Jiqing on 2016/9/28.
 */
public class KeyBoardUtils {
    /**
     * 隐藏键盘
     *
     * @param context
     * @param view
     */
    public static void hideKeyBoard(Context context, View view) {
            InputMethodManager imm = (InputMethodManager) context
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0); // 强制隐藏键盘
    }

    /**
     * 隐藏键盘
     *
     * @param context
     * @param windowToken
     */
    public static void hideKeyBoard(Context context, IBinder windowToken) {
        InputMethodManager imm = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(windowToken, 0); // 强制隐藏键盘
    }

    /**
     * 显示键盘
     *
     * @param context
     * @param view
     */
    public static void showKeyBoard(Context context, View view) {
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        view.requestFocus();
        view.findFocus();
        InputMethodManager imm = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }
}
